<?php

namespace App\Admin\Controllers;

use App\Models\User;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Illuminate\Http\Request;

class UserController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('车主')
            ->description('列表')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('Detail')
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('Edit')
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('Create')
            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new User);
        $grid->model()->orderBy("id", 'desc');

        $grid->id('Id');
        $grid->nickName('昵称');
        $grid->phone('手机');
        $grid->smallAvatar('头像')->display(function ($item) {
            return sprintf('<img src="%s" class="img-circle img-sm" />', $this->avatar);
        });
        $grid->sex('性别')->display(function () {
            return $this->sexText;
        });
        $grid->province('省份');
        $grid->city('城市');
        $grid->addTime('注册时间')->display(function ($item) {
            return $this->regtime > 0 ? date('Y-m-d H:i:s', $this->regtime) : "";
        });

        $grid->disableActions();
        $grid->disableCreateButton();
        $grid->disableRowSelector();

        $grid->filter(function (Grid\Filter $filters) {
            $filters->like('nickName', '昵称');
            $filters->like('phone', '手机');
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(User::findOrFail($id));

        $show->id('Id');
        $show->name('Name');
        $show->password('Password');
        $show->email('Email');
        $show->bigAvatar('BigAvatar');
        $show->middleAvatar('MiddleAvatar');
        $show->smallAvatar('SmallAvatar');
        $show->credit('Credit');
        $show->balance('Balance');
        $show->regtime('Regtime');
        $show->isDeled('IsDeled');
        $show->realName('RealName');
        $show->sex('Sex');
        $show->city('City');
        $show->province('Province');
        $show->address('Address');
        $show->birthday('Birthday');
        $show->contactEmail('ContactEmail');
        $show->qq('Qq');
        $show->msn('Msn');
        $show->aword('Aword');
        $show->domain('Domain');
        $show->nickName('NickName');
        $show->penName('PenName');
        $show->cardNumber('CardNumber');
        $show->roles('Roles');
        $show->resume('Resume');
        $show->phone('Phone');
        $show->detailAddress('DetailAddress');
        $show->zipcode('Zipcode');
        $show->relateSourceId('RelateSourceId');
        $show->regSource('RegSource');
        $show->isCheckedEmail('IsCheckedEmail');
        $show->isSigned('IsSigned');
        $show->isInvoked('IsInvoked');
        $show->invokeCode('InvokeCode');
        $show->invokeTime('InvokeTime');
        $show->schoolAge('SchoolAge');
        $show->school('School');
        $show->className('ClassName');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new User);

        $form->text('name', 'Name');
        $form->password('password', 'Password');
        $form->email('email', 'Email');
        $form->text('bigAvatar', 'BigAvatar');
        $form->text('middleAvatar', 'MiddleAvatar');
        $form->text('smallAvatar', 'SmallAvatar');
        $form->text('credit', 'Credit');
        $form->decimal('balance', 'Balance')->default(0.00);
        $form->number('regtime', 'Regtime');
        $form->switch('isDeled', 'IsDeled');
        $form->text('realName', 'RealName');
        $form->number('sex', 'Sex');
        $form->text('city', 'City');
        $form->text('province', 'Province');
        $form->text('address', 'Address');
        $form->text('birthday', 'Birthday');
        $form->text('contactEmail', 'ContactEmail');
        $form->text('qq', 'Qq');
        $form->text('msn', 'Msn');
        $form->text('aword', 'Aword');
        $form->text('domain', 'Domain');
        $form->text('nickName', 'NickName');
        $form->text('penName', 'PenName');
        $form->text('cardNumber', 'CardNumber');
        $form->text('roles', 'Roles');
        $form->text('resume', 'Resume');
        $form->mobile('phone', 'Phone');
        $form->text('detailAddress', 'DetailAddress');
        $form->text('zipcode', 'Zipcode');
        $form->text('relateSourceId', 'RelateSourceId');
        $form->switch('regSource', 'RegSource');
        $form->switch('isCheckedEmail', 'IsCheckedEmail');
        $form->switch('isSigned', 'IsSigned');
        $form->switch('isInvoked', 'IsInvoked');
        $form->text('invokeCode', 'InvokeCode');
        $form->number('invokeTime', 'InvokeTime');
        $form->switch('schoolAge', 'SchoolAge')->default(3);
        $form->text('school', 'School');
        $form->text('className', 'ClassName');

        return $form;
    }

    public function selectJson(Request $request)
    {
        $q = $request->get('q');

        $users = User::where(function ($query) use ($q) {
            $query->where("name", "like", "%$q%")->orWhere("id", "like", "%$q%");
        })->paginate(null, ['id', 'name as text']);

        foreach ($users->getIterator() as &$value) {
            $value->text = sprintf("%s【%s】", $value->text, $value->id);
        }

        return $users;

    }
}
